package com.xcc.redis.template;

import com.xcc.redis.config.MyRedisConfiguration;
import com.xcc.redis.config.MyRedisProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 配置starter的出口
 */
@Configuration
@ConditionalOnClass({RedisOperations.class})
@Import({MyRedisProperties.class,MyRedisConfiguration.class})
public class RedisTemplateConfiguration {

    @Bean
    @ConditionalOnMissingBean //当容器下有当前这个bean就不加载没有则加载
    public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

}
